package com.martin.videoplayer.video;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VideoUploadAdapter extends RecyclerView.Adapter<VideoUploadAdapter.Vh> {

    private Context mContext;
    private List<ActiveImageBean> mList = new ArrayList<>();
    private LayoutInflater mInflater;
    private ActionListener mActionListener;
    private boolean isVideo;
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    private int selectMax = 10;

    public VideoUploadAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setSelectMax(int selectMax) {
        this.selectMax = selectMax;
    }

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }

    public void insertList(List<String> imagePathList) {
        mList.clear();
        for (int i = 0; i < imagePathList.size(); i++) {
            mList.add(new ActiveImageBean(imagePathList.get(i)));
        }
        notifyDataSetChanged();
    }

    private List<ChooseVideoBean> tempVideoList = new ArrayList<>();

    public List<ChooseVideoBean> getTempVideoList() {
        return tempVideoList;
    }

    public void insertVideoList(List<ChooseVideoBean> videoBeanList) {
        tempVideoList = videoBeanList;
        mList.clear();
        for (int i = 0; i < videoBeanList.size(); i++) {
            ChooseVideoBean chooseVideoBean = videoBeanList.get(i);
            mList.add(new ActiveImageBean(chooseVideoBean.getType() == ChooseVideoBean.FILE ? chooseVideoBean.getVideoFile().getPath() : chooseVideoBean.getVideoUrl(), videoBeanList.get(i).getVideoPic(), videoBeanList.get(i).getDurationString()));
        }
        notifyDataSetChanged();
    }


    public void deleteItem(int position) {
        if (mList.size() > 0) {
            mList.remove(position);
            if (isVideo) {
                tempVideoList.remove(position);
            }
        }
        if (mList.size() == 0 && mActionListener != null) {
            mActionListener.onViewChange();
            mActionListener.onDeleteAll();
            tempVideoList.clear();
        }
        notifyDataSetChanged();
    }

    public List<String> getImageFileList() {
        List<String> list = new ArrayList<>();
        for (ActiveImageBean bean : mList) {
            if (bean.getmImage() != null) {
                list.add(bean.getmImage());
            }
        }
        return list;
    }

    public ArrayList<String> getVideoFileList() {
        ArrayList<String> list = new ArrayList<>();
        for (ActiveImageBean bean : mList) {
            if (bean.getmImage() != null) {
                list.add(bean.getmImage());
            }
        }
        return list;
    }

    public int getImageFileCount() {
        int count = 0;
        for (ActiveImageBean bean : mList) {
            if (bean.getmImage() != null) {
                count++;
            }
        }
        return count;
    }

    @NonNull
    @Override
    public Vh onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        return new Vh(mInflater.inflate(R.layout.item_video_up, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull Vh vh, int position) {
        if (getItemViewType(position) == TYPE_CAMERA) {
            vh.mIconAdd.setVisibility(View.VISIBLE);
            vh.delete_image.setVisibility(View.INVISIBLE);
            vh.tv_video_time.setText("");
            vh.mIconVideo.setVisibility(View.GONE);
            vh.mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mActionListener != null) {
                        mActionListener.onAddClick();
                    }
                }
            });
        } else {
            vh.mImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mActionListener != null) {
                        mActionListener.onItemClick(position);
                    }

                }
            });
            vh.setData(mList.get(position), position);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }

    private boolean isShowAddItem(int position) {
        int size = mList == null ? 0 : mList.size();
        return position == size;
    }

    public class Vh extends RecyclerView.ViewHolder {

        ImageView mIconAdd;
        ImageView mImg, mIconVideo;
        ImageView delete_image;
        TextView tv_video_time;

        public Vh(@NonNull View itemView) {
            super(itemView);
            mIconAdd = itemView.findViewById(R.id.icon_add);
            mImg = itemView.findViewById(R.id.img);
            delete_image = itemView.findViewById(R.id.delete_image);
            tv_video_time = itemView.findViewById(R.id.tv_video_time);
            mIconVideo = itemView.findViewById(R.id.iv_video_play);
        }

        void setData(ActiveImageBean bean, int position) {
            //itemView 的点击事件
            /*itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if (mActionListener != null) {
                        mActionListener.onItemClick(adapterPosition);
                    }
                }
            });*/
            delete_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = getAdapterPosition();
                    deleteItem(index);
                    /*int index = getAdapterPosition();
                    if (index != RecyclerView.NO_POSITION) {
                        mList.remove(index);
                        *//*notifyItemRemoved(index);
                        notifyItemRangeChanged(index, mList.size());*//*
                        notifyDataSetChanged();
                    }*/
                }
            });
            delete_image.setVisibility(View.VISIBLE);
            String imagFile = bean.getmImage() + "";
            if (imagFile.startsWith("http")) {
                ImgLoader.display(mContext, TextUtils.isEmpty(bean.getVideoPic()) ? imagFile : bean.getVideoPic(), mImg);
            } else {
                if (isVideo) {
                    ImgLoader.displayVideoThumb(mContext, new File(imagFile), mImg);
                    tv_video_time.setText(bean.getVideoTime());
                } else {
                    ImgLoader.display(mContext, new File(imagFile), mImg);
                }
            }
            if (isVideo) {
                mIconVideo.setVisibility(View.VISIBLE);
            } else {
                mIconVideo.setVisibility(View.GONE);
            }
        }
    }

    public void setVideo(boolean video) {
        isVideo = video;
    }

    public interface ActionListener {
        void onAddClick();

        void onItemClick(int position);

        void onDeleteAll();

        void onViewChange();
    }
}
